//
//  OrderCartView.m
//  WeiYiMei
//
//  Created by iMac on 2019/4/13.
//  Copyright © 2019年 liyefu. All rights reserved.
//  高度自适应：否

#import "OrderCartView.h"

@interface OrderCartView ()

@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UIView *sep;


@property (nonatomic, strong) UILabel *priceLab;
@property (nonatomic, strong) UILabel *VipPriceLab;


@property (nonatomic, strong) UIButton *selectButton;
@property (nonatomic, strong) UIButton *selectAction;

@end

@implementation OrderCartView

- (void)setType:(OrderCartViewType)type
{
    _type = type;
    if (type == OrderCartViewNormal) {
        [self.selectButton removeFromSuperview];
        self.VIPPayLab.hidden = YES;
        self.VIPPayPriceLab.hidden = YES;
        self.payLab.hidden = NO;
        self.payPriceLab.hidden = NO;
        self.goodsNumSelectView.hidden = YES;
        [self.goodsNumSelectView removeFromSuperview];
        self.tradeNumLab.hidden = NO;
        self.priceLab.hidden = NO;
        self.VipPriceLab.hidden = NO;
        
        [self.VipPriceLab mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.tradeNumLab.mas_bottom).offset(kWidth(10));
            make.top.equalTo(self.priceLab);
            make.right.equalTo(self).offset(-kWidth(10));
        }];
        
        [self.priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.VipPriceLab);
            make.top.equalTo(self.tradeNumLab.mas_bottom).offset(kWidth(10));
            make.right.equalTo(self.VipPriceLab.mas_left).offset(-kWidth(20));
        }];
        
        
        [self.payPriceLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.priceLab.mas_bottom).offset(kWidth(10));
            make.right.equalTo(self).offset(-kWidth(10));
        }];
        
        [self.payLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.payPriceLab);
            make.right.equalTo(self.payPriceLab.mas_left);
        }];
        [self layoutIfNeeded];
    }
    else {
        [self addSubview:self.selectButton];
        self.VIPPayLab.hidden = YES;
        self.VIPPayPriceLab.hidden = YES;
        self.payLab.hidden = YES;
        self.payPriceLab.hidden = YES;
        self.goodsNumSelectView.hidden = NO;
        self.tradeNumLab.hidden = YES;
        [self.tradeNumLab removeFromSuperview];
        self.priceLab.hidden = NO;
        self.VipPriceLab.hidden = NO;
        self.selectAction = [[UIButton alloc] init];
        self.selectAction.backgroundColor = [UIColor clearColor];
        [self.selectAction addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.selectAction];
        [self.selectAction mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.equalTo(self);
            make.top.equalTo(self.sep.mas_bottom);
            make.right.equalTo(self.tradeIcon.mas_left);
        }];
        [self.selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(kWidth(7));
            make.top.equalTo(self.sep.mas_bottom).offset(kWidth(26));
            make.width.height.equalTo(@18);
        }];
        [self.priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
            //        make.left.equalTo(self.tradeNameLab);
            make.bottom.equalTo(self).offset(-kWidth(15));
            make.right.equalTo(self.VipPriceLab.mas_left).offset(-kWidth(20));
        }];
        
        [self.VipPriceLab mas_makeConstraints:^(MASConstraintMaker *make) {
            //        make.left.equalTo(self.priceLab.mas_right).offset(kWidth(20));
            make.bottom.equalTo(self).offset(-kWidth(15));
            make.right.lessThanOrEqualTo(self).offset(-kWidth(10));
        }];
        [self layoutIfNeeded];
    }
}

- (void)setPrice:(NSString *)price
{
    _price = price;
    NSString *temp = [NSString stringWithFormat:@"原价:￥%.2f",price.floatValue];
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:temp];
    [attri setAttributes:@{NSForegroundColorAttributeName:kColor888888,NSFontAttributeName:[UIFont systemFontOfSize:12]} range:NSMakeRange(0, 3)];
    [attri setAttributes:@{NSForegroundColorAttributeName:kColor333333,NSFontAttributeName:[UIFont systemFontOfSize:12]} range:NSMakeRange(3, temp.length-3)];
    self.priceLab.attributedText = attri;
}

- (void)setMemberPrice:(NSString *)memberPrice
{
    _memberPrice = memberPrice;
    #warning 会员价显示控制
//    if (UserUtils.shared.showVip) {
        NSString *temp = [NSString stringWithFormat:@"会员价:￥%.2f",memberPrice.floatValue];
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:temp];
        [attri setAttributes:@{NSForegroundColorAttributeName:kColor888888,NSFontAttributeName:[UIFont systemFontOfSize:12]} range:NSMakeRange(0, 4)];
        [attri setAttributes:@{NSForegroundColorAttributeName:kColorFF2C2C,NSFontAttributeName:[UIFont systemFontOfSize:12]} range:NSMakeRange(4, temp.length-4)];
        self.VipPriceLab.attributedText = attri;
//    }
//    else {
//        self.VipPriceLab.text = @"";
//        [self.priceLab mas_updateConstraints:^(MASConstraintMaker *make) {
//            //            make.top.equalTo(self.VipPriceLab);
////            make.top.equalTo(self.tradeNumLab.mas_bottom).offset(kWidth(10));
////            make.right.equalTo(self.VipPriceLab.mas_left).offset(-kWidth(20));
//            make.right.equalTo(self).offset(-kWidth(10));
//        }];
//    }

}

- (void)selectAction:(UIButton *)sender
{
//    self.selectButton.selected = YES;
    if (self.selectHandler) {
        self.selectHandler(self.goodsNumSelectView.num);
    }
}

- (void)setIsSelect:(BOOL)isSelect
{
    _isSelect = isSelect;
    self.selectButton.selected = isSelect;
}

- (void)setupSubViews
{
    self.goodsNumSelectView = [[GoodsNumSelectView alloc] init];
    self.goodsNumSelectView.layer.borderColor = kColorDDD.CGColor;
    self.goodsNumSelectView.layer.borderWidth = 1;
    self.goodsNumSelectView.layer.cornerRadius = 3;
    self.goodsNumSelectView.layer.masksToBounds = YES;
    [self addSubview:self.goodsNumSelectView];
    WeakSelf
    self.goodsNumSelectView.addOrDeleteHandler = ^(int num) {
        if (weakSelf.addOrDeleteHandler) {
            weakSelf.addOrDeleteHandler(num);
        }
    };
    
    
    self.orderState = [[UILabel alloc] init];
    self.orderState.textColor = kColor333333;
    self.orderState.font = [UIFont systemFontOfSize:14];
    [self addSubview:self.orderState];
    
    self.icon = [[UIImageView alloc] init];
    self.icon.image = [UIImage imageNamed:@"store"];
    [self addSubview:self.icon];
    
    self.shopNameLab = [[UILabel alloc] init];
    self.shopNameLab.textColor = kColor333333;
    self.shopNameLab.font = [UIFont boldSystemFontOfSize:14];
    [self addSubview:self.shopNameLab];
    
    self.tradeIcon = [[UIImageView alloc] init];
    self.tradeIcon.image = [UIImage imageNamed:@"pic_moren"];
    self.tradeIcon.layer.cornerRadius = 4;
    self.tradeIcon.layer.masksToBounds = YES;
    [self addSubview:self.tradeIcon];
    
    self.tradeNameLab = [[UILabel alloc] init];
    self.tradeNameLab.textColor = kColor333333;
    self.tradeNameLab.font = [UIFont boldSystemFontOfSize:14];
    [self addSubview:self.tradeNameLab];
    
    self.tradeDesLab = [[UILabel alloc] init];
    self.tradeDesLab.textColor = kColor666666;
    self.tradeDesLab.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.tradeDesLab];
    
    self.tradeTimeLab = [[UILabel alloc] init];
    self.tradeTimeLab.textColor = kColor666666;
    self.tradeTimeLab.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.tradeTimeLab];
    
    self.tradeNumLab = [[UILabel alloc] init];
    self.tradeNumLab.textColor = kColor666666;
    self.tradeNumLab.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.tradeNumLab];
    
    self.payLab = [[UILabel alloc] init];
    self.payLab.textColor = kColor333333;
    self.payLab.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.payLab];
    
    self.payPriceLab = [[UILabel alloc] init];
    self.payPriceLab.textColor = kColorFF2C2C;
    self.payPriceLab.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.payPriceLab];
    
    self.sep = [[UIView alloc] init];
    self.sep.backgroundColor = kColorDDD;
    [self addSubview:self.sep];
    
    self.VIPPayLab = [[UILabel alloc] init];
    self.VIPPayLab.textColor = kColor333333;
    self.VIPPayLab.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.VIPPayLab];
    
    self.VIPPayPriceLab = [[UILabel alloc] init];
    self.VIPPayPriceLab.textColor = kColorFF2C2C;
    self.VIPPayPriceLab.font = [UIFont boldSystemFontOfSize:12];
    [self addSubview:self.VIPPayPriceLab];
    
    self.priceLab = [[UILabel alloc] init];
    [self addSubview:self.priceLab];
    
    self.VipPriceLab = [[UILabel alloc] init];
    [self addSubview:self.VipPriceLab];
    
    self.selectButton = [[UIButton alloc] init];
    [self.selectButton setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
    [self.selectButton setImage:[UIImage imageNamed:@"checked"] forState:UIControlStateSelected];
    [self addSubview:self.selectButton];
    
    self.payLab.text = @"共0件，合计：";
    self.payPriceLab.text = @"￥0.00";
//    self.VIPPayLab.text = @"会员价";
//    self.VIPPayPriceLab.text = @"￥0.00";
}

- (void)setupConstraints
{
    [self.sep mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(kWidth(15));
        make.right.equalTo(self).offset(-kWidth(15));
        make.top.equalTo(self).offset(kWidth(45));
        make.height.equalTo(@(1.0/kScale));
    }];
    
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(kWidth(15));
        make.top.equalTo(self).offset(kWidth(10));
    }];
    
    [self.orderState mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.icon);
        make.right.equalTo(self).offset(-kWidth(15));
    }];
    
    [self.shopNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.icon);
        make.left.equalTo(self.icon.mas_right).offset(kWidth(10));
        make.right.lessThanOrEqualTo(self.orderState.mas_left).offset(-kWidth(10));
    }];
    
    
    
    [self.tradeIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sep.mas_bottom).offset(kWidth(15));
        make.left.equalTo(self).offset(kWidth(15)).priority(200);
        make.width.equalTo(@(kWidth(115)));
        make.height.equalTo(@(75/115.0*kWidth(115)));
//        make.bottom.equalTo(self).offset(-kWidth(43));
        make.left.equalTo(self.selectButton.mas_right).offset(kWidth(13));
    }];
    
    [self.tradeNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tradeIcon);
        make.left.equalTo(self.tradeIcon.mas_right).offset(kWidth(9));
        make.right.lessThanOrEqualTo(self).offset(-kWidth(10));
    }];
    
    [self.tradeDesLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tradeNameLab.mas_bottom).offset(kWidth(10));
        make.left.equalTo(self.tradeNameLab);
        make.right.lessThanOrEqualTo(self).offset(-kWidth(10));
    }];
    
    [self.tradeTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tradeNameLab);
        make.top.equalTo(self.tradeDesLab.mas_bottom).offset(kWidth(15));
//        make.width.equalTo(@(kWidth(100)));
        make.right.lessThanOrEqualTo(self.tradeNumLab.mas_left).offset(-kWidth(10)).priority(200);
        make.right.lessThanOrEqualTo(self.goodsNumSelectView.mas_left).offset(-kWidth(10));
    }];
    
    [self.tradeNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.tradeTimeLab);
        make.right.equalTo(self).offset(-kWidth(10));
    }];
    
    [self.goodsNumSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tradeDesLab.mas_bottom).offset(kWidth(11));
        make.right.equalTo(self).offset(-kWidth(12));
        make.width.equalTo(@(kWidth(80)));
        make.height.equalTo(@(kWidth(21)));
    }];
    
    [self.VIPPayLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tradeNameLab);
        make.bottom.equalTo(self).offset(-kWidth(10));
    }];
    
    [self.VIPPayPriceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.VIPPayLab.mas_right);
        make.centerY.equalTo(self.VIPPayLab);
    }];
    
    
}

@end
